public class num106 {
    int index = 0;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        int[] arr = new int[postorder.length];
        for (int i = 0; i < postorder.length; i++) {
            arr[i] = postorder[postorder.length - i - 1];
        }
        return postorderBildTree(inorder,arr,0,arr.length - 1);
    }
    private TreeNode postorderBildTree(int[] inorder,int[] arr,int left,int right){
        if(left > right){
            return null;
        }
        if(index == inorder.length){
            return null;
        }
        TreeNode root = new TreeNode(arr[index]);
        index++;
        int pos = find(inorder,root.val);
        root.right = postorderBildTree(inorder,arr,pos + 1,right);
        root.left = postorderBildTree(inorder,arr,left,pos - 1);
        return root;
    }
    public int find(int[] inorder,int val){
        for (int i = 0; i < inorder.length; i++) {
            if(inorder[i] == val){
                return i;
            }
        }
        return -1;
    }
}
